//
//  RootView.m
//  无限循环滚动
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 ER. All rights reserved.
//

#import "RootView.h"
#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height
@interface RootView ()<UIScrollViewDelegate>
@property (retain,nonatomic)UIScrollView *bigScro;
@property (retain,nonatomic)UIScrollView *smallScro;
@property (retain, nonatomic)UIImageView *lastImageView;
@property (retain, nonatomic)UIImageView *firstImageView;

@property (retain, nonatomic)UIScrollView *scroview0;
@property (retain, nonatomic)UIScrollView *scroview1;
@property (retain, nonatomic)UIScrollView *scroview2;
@property (retain, nonatomic)UIScrollView *scroview3;
@property (retain, nonatomic)UIScrollView *scroview4;
@property (retain, nonatomic)NSTimer *timmer;
@property (retain, nonatomic)NSArray *array;
@end

@implementation RootView
- (instancetype)initWithFrame:(CGRect)frame withNSArray:(NSArray *)array{
    self = [super initWithFrame:frame];
    if (self) {
        self.array = array;
        [self createSubviews];
        [self startTimmer];
    }
    return self;
}
//- (instancetype)initWithFrame:(CGRect)frame
//{
//    self = [super initWithFrame:frame];
//    if (self) {
//        [self createSubviews];
//        [self startTimmer];
//    }
//    return self;
//}
- (void)stop{
    [self.timmer invalidate];
}
- (void)startTimmer
{
  self.timmer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(play) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timmer forMode:NSRunLoopCommonModes];
}
 - (void)play
{
    if (self.page.currentPage == self.array.count-1) {
        self.page.currentPage = 0;
    }
    else{
    self.page.currentPage += 1;
    }
    self.bigScro.contentOffset = CGPointMake(WIDTH*(self.page.currentPage+1), 0);
}
- (void)createSubviews
{
    self.bigScro = [[UIScrollView alloc] initWithFrame:self.frame];
    self.bigScro.contentSize = CGSizeMake(WIDTH*(self.array.count+2), HEIGHT);
    self.bigScro.contentOffset = CGPointMake(WIDTH, 0);
    self.bigScro.pagingEnabled = YES;
    self.bigScro.delegate = self;
    self.bigScro.bounces = NO;
   #pragma  边界回弹
    /**
      self.bigScro.bounds = NO;
     */
   
    [self addSubview:self.bigScro];
//    /** R0*/
//    self.scroview0 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
//    self.scroview0.delegate = self;
//    self.scroview0.tag =10000;
//    self.scroview0.minimumZoomScale = 0.4;
//    self.scroview0.maximumZoomScale = 2;
//    self.scroview0.backgroundColor = [UIColor redColor];
//    [self.bigScro addSubview:self.scroview0];
//    
//
//        UIImageView *imag0 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
//        imag0.tag = 200000;
//        imag0.image = [UIImage imageNamed:@"12.png"];
//        [self.scroview0 addSubview:imag0];
//    
//    
//    //    /** R1*/
//    self.scroview1 = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT)
//                      ];
//    self.scroview1.delegate = self;
//    self.scroview1.tag =10001;
//    self.scroview1.minimumZoomScale = 0.1;
//    self.scroview1.maximumZoomScale = 2;
//    [self.bigScro addSubview:self.scroview1];
//        UIImageView *imag1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
//        imag1.image = [UIImage imageNamed:@"10.png"];
//    imag1.tag = 1;
//        [self.scroview1 addSubview:imag1];
//    
//   /** R2*/
//    
//    self.scroview2 = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH*2, 0, WIDTH, HEIGHT)];
//    [self.bigScro addSubview:self.scroview2];
//    self.scroview2.delegate = self;
//    self.scroview2.tag = 10002;
//    self.scroview2.minimumZoomScale = 0.1;
//    self.scroview2.maximumZoomScale = 2;
//    
//    UIImageView *imag2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
//    imag2.image = [UIImage imageNamed:@"11.png"];
//    [self.scroview2 addSubview:imag2];
//    /** R3*/
//    self.scroview3 = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH*3, 0, WIDTH, HEIGHT)];
//    [self.bigScro addSubview:self.scroview3];
//    self.scroview3.delegate = self;
//    self.scroview3.tag = 10003;
//    self.scroview3.minimumZoomScale = 0.1;
//    self.scroview3.maximumZoomScale = 2;
//
//    
//    UIImageView *imag3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
//    imag3.image = [UIImage imageNamed:@"12.png"];
//    [self.scroview3 addSubview:imag3];
//    /** R4*/
//    self.scroview4 = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH*4, 0, WIDTH, HEIGHT)];
//    [self.bigScro addSubview:self.scroview4];
//    self.scroview4.tag = 10004;
//    
//    UIImageView *imag4 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
//    imag4.image = [UIImage imageNamed:@"10.png"];
//    [self.scroview4 addSubview:imag4];
//    self.scroview4.delegate = self;
//    self.scroview4.minimumZoomScale = 0.1;
//    self.scroview4.maximumZoomScale = 2;
//
//   /**  R5*/
//    
    for (int i = 1; i<self.array.count+1; i++) {
        UIScrollView *scroSmall = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH*i, 0, WIDTH, HEIGHT)];
        [self.bigScro addSubview:scroSmall];
        scroSmall.tag = 10000+i+1;
        
        UIImageView *imageV = [[UIImageView alloc] init];
#pragma bug
        NSString *name = [self.array objectAtIndex:i-1];
//        imageV.image = [UIImage imageNamed:self.array[i]];
        NSLog(@"☀️%@",name);
#pragma bug imageV无frame
        imageV.image = [UIImage imageNamed:name];
        imageV.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
        [scroSmall addSubview:imageV];
        scroSmall.delegate = self;
        scroSmall.minimumZoomScale = 0.1;
        scroSmall.maximumZoomScale = 2;
    }
    /** last  (self.array.count+1)*/
    UIScrollView *lastScro = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH*(self.array.count+1), 0, WIDTH, HEIGHT)];
    [self.bigScro addSubview:lastScro];
    lastScro.delegate = self;
    lastScro.minimumZoomScale = 0.1;
    lastScro.maximumZoomScale = 2;
    UIImageView *lastImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    lastImageV.image = [UIImage imageNamed:[self.array objectAtIndex:0]];
    [lastScro addSubview:lastImageV];
    /** first*/
    UIScrollView *firstScro = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.bigScro addSubview:firstScro];
    firstScro.delegate = self;
    firstScro.minimumZoomScale = 0.1;
    firstScro.maximumZoomScale = 2;
    UIImageView *firstImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    firstImageV.image = [UIImage imageNamed:[self.array objectAtIndex:self.array.count-1]];
    [firstScro addSubview:firstImageV];
    
  /** page */
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, HEIGHT/20, WIDTH, HEIGHT/4)];
    //self.page.backgroundColor = [UIColor blackColor];
    self.page.numberOfPages = self.array.count;
    self.page.currentPageIndicatorTintColor = [UIColor orangeColor];
    self.page.pageIndicatorTintColor = [UIColor cyanColor];
    [self addSubview:self.page];
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
    
}
- (void)pageAction:(UIPageControl *)page
{
   // [self.timmer invalidate];
//    if (page.currentPage == 0) {
//        self.bigScro.contentOffset = CGPointMake(WIDTH, 0);
//    }if (page.currentPage == 1) {
//        self.bigScro.contentOffset = CGPointMake(WIDTH*2, 0);
//    }if (page.currentPage == 2) {
//        self.bigScro.contentOffset = CGPointMake(WIDTH*3, 0);
//    }else{
//        
//    }
    self.bigScro.contentOffset = CGPointMake(WIDTH*(page.currentPage+1), 0);
}
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [[scrollView subviews] firstObject];
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    [self stop];
    CGPoint center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    [[scrollView subviews] firstObject].center = center;
   }


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (self.bigScro.contentOffset.x == 0) {
        self.bigScro.contentOffset = CGPointMake(WIDTH*self.array.count, 0);
    }
    if (self.bigScro.contentOffset.x == WIDTH*(self.array.count+1)) {
        self.bigScro.contentOffset = CGPointMake(WIDTH, 0);
    }
 
        self.page.currentPage = self.bigScro.contentOffset.x/WIDTH-1;

//    if (self.bigScro.contentOffset.x == WIDTH) {
//        self.page.currentPage = 0;
//    }if (self.bigScro.contentOffset.x == 2*WIDTH) {
//        self.page.currentPage = 1;
//    }if (self.bigScro.contentOffset.x == 3*WIDTH) {
//        self.page.currentPage = 2;
//    }
    for (UIScrollView *sView in self.bigScro.subviews) {
        if ([sView isKindOfClass:[UIScrollView class]]) {
            [sView setZoomScale:1.0];
            NSLog(@"%@", sView);
        }
    }
    NSLog(@"风华绝代顺丰速递个数据;熬过瓦i 个;i%lf",self.bigScro.contentOffset.x);
    [self startTimmer];
    
  }

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //[self stop];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self stop];
}




@end
